package org.jeecg.modules.business.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.ResultCode;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.business.dto.BusMeetingRoomScheduleDetailDto;
import org.jeecg.modules.business.dto.BusMeetingRoomScheduleDto;
import org.jeecg.modules.business.entity.*;
import org.jeecg.modules.business.service.*;
import org.jeecg.modules.business.service.impl.BusMeetingRoomScheduleServiceImpl;
import org.jeecg.modules.business.service.impl.BusMeetingRoomServiceImpl;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会议室预定表 前端控制器
 * </p>
 *
 * @author fendo
 * @since 2023-03-15
 */
@RestController
@RequestMapping("/business/busMeetingRoomSchedule")
public class BusMeetingRoomScheduleController {

    @Resource
    private BusMeetingRoomScheduleServiceImpl roomScheduleService;
    @Resource
    private IBusMeetingRoomScheduleOrderDetailService busMeetingRoomScheduleOrderDetailService;
    @Resource
    private IBusMeetingRoomScheduleDetailService busMeetingRoomScheduleDetailService;
    @Resource
    private IBusRoomPayTypeService busRoomPayTypeService;
    @Resource
    private IBusRoomsLivingOrderService busRoomsLivingOrderService;
    @Resource
    private IBusCustomerService busCustomerService;

    @ApiOperation(value="创建会议预定", notes="创建会议预定")
    @PostMapping(value = "/create")
    public Result create(@RequestBody BusMeetingRoomScheduleDto dto){
        if(StringUtils.isBlank(dto.getHotelId())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getTheme())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getDestinedName())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getMobile())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(dto.getMobile())){
            return Result.error(ResultCode.PARAM_MISS);
        }
        List<BusMeetingRoomScheduleDetailDto> detailList = dto.getDetailList();

        if(CollectionUtil.isEmpty(detailList)){
            return Result.error(ResultCode.PARAM_MISS,"档期预定不能为空,请重新填入!");
        }
        /**
         * 验证参数是否正确
         */
        List<BusMeetingRoomScheduleDetailDto> distinctDetailList = detailList.stream().distinct().collect(Collectors.toList());

        if(distinctDetailList.size() != detailList.size()) {
            return Result.error(ResultCode.PARAM_VALID_ERROR,"数据重复!");
        }
        try {
            return roomScheduleService.create(dto);
        }catch (Exception e){
            return Result.error("数据重复,或者错误!");
        }
    }



    @ApiOperation(value="获取", notes="获取")
    @GetMapping(value = "/fetch")
    public Result fetch(String startDate,String endDate,String hotelId){
        if(StringUtils.isBlank(startDate)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(endDate)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        if(StringUtils.isBlank(hotelId)){
            return Result.error(ResultCode.PARAM_MISS);
        }
        try {
            return roomScheduleService.fetch(startDate,endDate,hotelId);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     *  编辑
     *
     * @param busMeetingRoomSchedule
     * @return
     */
    @AutoLog(value = "bus_meeting_room-编辑")
    @ApiOperation(value="bus_meeting_room-编辑", notes="bus_meeting_room-编辑")
    //@RequiresPermissions("business:bus_meeting_room:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody BusMeetingRoomSchedule busMeetingRoomSchedule) {
        roomScheduleService.updateById(busMeetingRoomSchedule);
        return Result.OK("编辑成功!");
    }

    /**
     * 分页列表查询
     *
     * @param busMeetingRoomSchedule
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "bus_market_coupons_cash_info-分页列表查询")
    @ApiOperation(value="bus_market_coupons_cash_info-分页列表查询", notes="bus_market_coupons_cash_info-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<BusMeetingRoomSchedule>> queryPageList(BusMeetingRoomSchedule busMeetingRoomSchedule,
                                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<BusMeetingRoomSchedule> queryWrapper = QueryGenerator.initQueryWrapper(busMeetingRoomSchedule, req.getParameterMap());
        Page<BusMeetingRoomSchedule> page = new Page<BusMeetingRoomSchedule>(pageNo, pageSize);
        IPage<BusMeetingRoomSchedule> pageList = roomScheduleService.page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            LambdaQueryWrapper<BusMeetingRoomScheduleOrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BusMeetingRoomScheduleOrderDetail::getMeetingRoomScheduleId, item.getId());
            List<BusMeetingRoomScheduleOrderDetail> meetingRoomScheduleOrderDetailList = busMeetingRoomScheduleOrderDetailService.list(lambdaQueryWrapper);
            if (ObjectUtils.isNotEmpty(meetingRoomScheduleOrderDetailList)) {
                item.setOrderDetailList(meetingRoomScheduleOrderDetailList);
                meetingRoomScheduleOrderDetailList.forEach(v -> {
                    BusRoomPayType busRoomPayType = busRoomPayTypeService.getById(v.getPayWay());
                    if (busRoomPayType != null) {
                        v.setPayWayName(busRoomPayType.getName());
                    }
                });
            }

            LambdaQueryWrapper<BusMeetingRoomScheduleDetail> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(BusMeetingRoomScheduleDetail::getMeetingRoomScheduleId, item.getId());
            lambdaQueryWrapper2.eq(BusMeetingRoomScheduleDetail::getStatus, 0);
            Long count = busMeetingRoomScheduleDetailService.count(lambdaQueryWrapper2);
            item.setConfirmStatus(count > 0 ? 0 : 1);
            if (StringUtils.isNotBlank(item.getSettleRoomOrderNo())) {
                BusRoomsLivingOrder order = busRoomsLivingOrderService.getById(item.getSettleRoomOrderNo());
                if (order != null) {
                    BusCustomer busCustomer = busCustomerService.getById(order.getContactId());
                    if (busCustomer != null) {
                        item.setContactName(busCustomer.getName());
                    }
                }
            }
        });
        return Result.OK(pageList);
    }

    /**
     *  结账
     *
     * @param busMeetingRoomSchedule
     * @return
     */
    @AutoLog(value = "bus_meeting_room-编辑")
    @ApiOperation(value="bus_meeting_room-编辑", notes="bus_meeting_room-编辑")
    //@RequiresPermissions("business:bus_meeting_room:edit")
    @PostMapping(value = "/settle")
    public Result<String> settle(@RequestBody BusMeetingRoomSchedule busMeetingRoomSchedule) {
        return roomScheduleService.settle(busMeetingRoomSchedule);
    }
}

